<?php
namespace App\Service;
use App\Common\Utill\OperationLog;
use App\Models\Pictures;
use Illuminate\Support\Facades\Log;

class PictureService extends CommonService
{
    public function __construct()
    {
        parent::__construct();
    }

    public function getListData($request,$num)
    {
        $model = new Pictures();
        $query = $model::query();
        $adminId = $this->adminId;
        //名称
        $query->where("is_del",1);
        $query->orderBy('id','desc');
        $result = $query->paginate($num)->toArray();
        foreach ($result['data'] as $key => $val) {

        }
        return  $this->success('SUCCESS',$result['total'],$result['data']);
    }


    /**
     * 添加数据保存
     * @return void
     */
    public function create($thumbs)
    {
        $model = new Pictures();
        $model->thumbs = $thumbs;
        $model->is_del  = 1;
        $model->use_numbers  = 0;
        $model->created_at  = date("Y-m-d H:i:s");
        if(!$model->save()){
            return $this->error('添加失败！！！');
        }else{
            OperationLog::record(1,'图片管理-添加',$model->id,['thumbs'=>$thumbs]);
            return $this->success('添加成功');
        }
    }

    /**
     * 编辑
     * @param $data
     * @return void
     */
    public function update($id,$thumbs){
        $oldData =Pictures::query()->where('id',$id)->first()->toArray();
        $model = Pictures::findOrFail($id);
        $model->thumbs = $thumbs ?? '';
        $model->updated_at  = date("Y-m-d H:i:s");
        if(!$model->save()){
            return $this->error('修改失败');
        }
        OperationLog::record(2,'图片管理-编辑',$id,['thumbs'=>$thumbs],$oldData);
        return $this->success("修改成功");
    }

    /**
     * 单个删除
     */
    public function delOne($id){
        $result = Pictures::findOrFail($id);
        $result->is_del = -1;
        $result->updated_at = date("Y-m-d H:i:s");
        if (!$result->save()) {
            return $this->error('删除失败');
        }
        OperationLog::record(3,'图片管理-删除',$id);
        return $this->success("删除成功");
    }

    /**
     * 批量删除
     * @return void
     */
    public function delAll($idArr)
    {
        if(!$idArr){
            return $this->error('删除内容不能为空');
        }
        Log::info("删除id:",$idArr);
        $delData['is_del'] = -1;
        $delData['updated_at'] = date("Y-m-d H:i:s");
        $result = Pictures::query()->whereIn("id",$idArr)->update($delData);
        if(!$result){
            return $this->error('删除失败');
        }
        OperationLog::record(3,'图片管理-删除',0,'','',implode(",", $idArr));
        return $this->success("删除成功");
    }


}
